using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace Ribbon
{
	public class RabTool : Control
	{
		private List<UIElement> _Items = new List<UIElement>();

		public static readonly DependencyProperty TextHideProperty;

		public List<UIElement> Items
		{
			get
			{
				return _Items;
			}
			set
			{
				_Items = value;
			}
		}

		public bool TextHide
		{
			get
			{
				return (bool)GetValue(TextHideProperty);
			}
			set
			{
				SetValue(TextHideProperty, value);
			}
		}

		static RabTool()
		{
			TextHideProperty = DependencyProperty.Register("TextHide", typeof(bool), typeof(RabTool), new PropertyMetadata(true, OnTextHideChanged));
			FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(typeof(RabTool), new FrameworkPropertyMetadata(typeof(RabTool)));
		}

		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			StackPanel stackPanel = GetTemplateChild("PART_Items") as StackPanel;
			if (stackPanel == null || Items == null)
			{
				return;
			}
			foreach (UIElement item in Items)
			{
				stackPanel.Children.Add(item);
			}
		}

		private static void OnTextHideChanged(object sender, DependencyPropertyChangedEventArgs args)
		{
			RabTool rabTool = sender as RabTool;
			if (rabTool == null)
			{
				return;
			}
			foreach (RabGroup item in rabTool.Items)
			{
				item.TextHide = ((!rabTool.TextHide) ? Visibility.Collapsed : Visibility.Visible);
			}
			if (!rabTool.TextHide)
			{
				rabTool.Height = rabTool.ActualHeight * 3.0 / 5.0;
			}
			else
			{
				rabTool.Height = rabTool.ActualHeight * 5.0 / 3.0;
			}
		}
	}
}
